package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsArticle;
import com.qf.service.ArticleService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/24 22:38
 */
@RestController
@RequestMapping("/admin/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * http://localhost:8083/admin/article/list?page=1&limit=20&sort=add_time&order=desc
     * 分页查询
     */
    @RequiresPermissions(value = {"admin:article:list"})
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button = "查询")
    @GetMapping("/list")
    public Object list(
            String title,
            String sort,
            String order,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer limit
    ){
        IPage<DtsArticle> ipage = articleService.findListByQuery(title,sort,order,page,limit);
        Map data = new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * /create
     */
    @RequiresPermissions(value = "admin:article:create")
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsArticle dtsArticle){
        dtsArticle = articleService.create(dtsArticle);
        return ResponseUtil.ok(dtsArticle);
    }

    /**
     * update
     */
    @RequiresPermissions(value = "admin:article:update")
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsArticle dtsArticle){
        dtsArticle = articleService.update(dtsArticle);
        return ResponseUtil.ok(dtsArticle);
    }

    /**
     * delete
     */
    @RequiresPermissions(value = "admin:article:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","公告列表"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsArticle dtsArticle){
        dtsArticle = articleService.delete(dtsArticle);
        return ResponseUtil.ok(dtsArticle);
    }
}
